<template>
  <form-dialog
    :title="title"
    :visible="visible"
    @handleClose="handleClose"
  >
    <template #content>
      <div class="tip-container">
        <i class="icon iconfont iconicon_gaojing" style="color:#fe983d"></i>
        <span class="text">{{ tip }}</span>
      </div>
      <slot name="content" />
      <div class="message-container">
        <span>{{ message }}</span>
      </div>
      <div class="form-container">
        <el-form ref="form" :model="formData" :rules="formRules">
          <el-form-item :label="$t('resource.DeletePassword')" prop="password" :label-width="language === 'zh' ? '80px' : '100px'">
            <el-input type="password" v-model="formData.password" show-password @keyup.enter.native="submit" :placeholder="$t('common.passwordDeletePlaceholder')"></el-input>
          </el-form-item>
        </el-form>
      </div>
    </template>
    <template #footer>
      <el-button @click="handleClose">{{ $t('common.cancel') }}</el-button>
      <el-button type="primary" @click="submit" :disabled="loading">{{ $t('common.confirm') }}</el-button>
    </template>
  </form-dialog>
</template>
<script lang="ts">
import Vue from 'vue';
import FormDialog from '../../../components/Dialog/formDialog.vue';
import { AuthModule } from '../../../store/modules/auth';
import orinusApi from '../../../api/orinusApi';
import { AppModule } from '../../../store/modules/app';
export default Vue.extend({
  components: { FormDialog },
  data() {
    return {
      visible: false,
      loading: false,
      meta: {},
      title: "",
      tip: "",
      message: "",
      orderList: [],
      formData: {
        password: ""
      },
      formRules: {
        password: [
          { required: true, message: (this as any).$t('resource.deletePasswordRequired'), trigger: 'blur' },
          { validator: (rule: any, value: any, callback: any) => {
            (this as any).$systemTunnyApi.apiCheckPassword(value).then((result: any) => {
              if (result.check_password_flag) {
                return callback();
              } else {
                return callback(new Error((this as any).$t('resource.deletePasswordCheckFailed')));
              }
            }).catch(() => {
              return callback(new Error((this as any).$t('resource.deletePasswordCheckFailed')));
            });
          }, trigger: 'blur' }
        ]
      },
      language: AppModule.language,
    }
  },
  
  methods: {
    init({
      meta,
      title,
      tip,
      message,
      orderList
    }: any) {
      // console.log(orderList);
      (this as any).meta = meta;
      (this as any).title = title;
      (this as any).tip = tip;
      (this as any).message = message;
      (this as any).visible = true;
      (this as any).orderList = orderList;
      this.$nextTick(() => {
        if (this.$refs['form']) {
          (this.$refs['form'] as any).clearValidate();
        }
      })
    },
    handleClose() {
      (this as any).visible = false;
      (this as any).loading = false;
      (this as any).meta = {};
      (this as any).title = "";
      (this as any).tip = "";
      (this as any).message = "";
      (this as any).formData = {
        password: ""
      };
      if (this.$refs['form']) {
        (this.$refs['form'] as any).clearValidate();
      }
    },
    submit() {
      (this as any).$refs['form'].validate((valid: any) => {
        if (valid) {
          let promiseArray: any = [];
          (this as any).loading = true;
          // console.log(this.orderList);
          this.orderList.forEach((item: any) => {
            promiseArray.push((this as any).$orinusApi.deleteOrder(item.id));
          })
          Promise.all(promiseArray).then((res: any) => {
            this.$emit("success");
            (this as any).handleClose();
            (this as any).loading = false;
            this.$message({
              message: this.$t("message.requestSuccess").toString(),
              type: "success",
            });
          }).catch(err => {
            (this as any).loading = false;
            this.$message({
              message: this.$t("message.actionFail").toString(),
              type: "success",
            });
          });
        }
      });
    }
  }
})
</script>
<style lang="scss" scoped>
  .tip-container {
    .icon {
      width: 80px;
      padding-right: 20px;
      display: inline-block;
      text-align: right;
      &::before {
        font-size: 20px;
        color:#fe983d;
      }
    }

    .text {
      color: #333333;
      font-size: 16px;
    }
  }
  .message-container {
    color: #999999;
    font-size: 12px;
    padding-left: 80px;
    margin: 10px 0px;
  }
</style>
